package com.sx.micowell.framework.tools;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 保存图片工具类
 */
public class CutUtil {
    private static String imageCachePath;
    private static final String CACHE_PATH_IMAGE = "image";


    private static void refreshGallery(Context context, File file) {
        if (file.exists()) {
            Uri uri = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            intent.setData(uri);
            context.sendBroadcast(intent);
        }
    }

    private static boolean isEmptyBitmap(Bitmap src) {
        return src == null || src.getWidth() == 0 || src.getHeight() == 0;
    }

    private static boolean saveToFile(Bitmap bitmap, File file) {
        if (isEmptyBitmap(bitmap)) {
            return false;
        }
        OutputStream os = null;
        boolean isSuccess = false;
        try {
            os = new BufferedOutputStream(new FileOutputStream(file));
            isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
            if (!bitmap.isRecycled()) {
                bitmap.recycle();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isSuccess;
    }

    private static void saveToFile(Context context, Bitmap bitmap, String fileName) {
        File file = new File(getImageCachePath(context), fileName);
        try {
            if (file.exists()) {
                file.delete();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            boolean ret = saveToFile(bitmap, file);
            if (ret) {
                //Toast.makeText(context, "截图已保持至 " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
                Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
            }
            //发送广播通知图库更新
            refreshGallery(context, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Bitmap shotScrollView(View scrollView) {
        int h = 0;
        Bitmap bitmap = null;
//        for (int i = 0; i < scrollView.getHeight(); i++) {
//            h += scrollView.getHeight();
//            scrollView.setBackgroundColor(Color.parseColor("#ffffff"));
//        }
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), scrollView.getHeight(), Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);
        return bitmap;
    }

    public static void shotView(View view) {
        Bitmap bitmap = shotScrollView(view);
        saveToFile( view.getContext(), bitmap, "PaymentCode.jpg");
    }

    public static Bitmap shotBitmap(View view) {
        return shotScrollView(view);
    }

    /**
     * 获取图像消息缓存地址
     */
    public static String getImageCachePath(Context context) {
        if (imageCachePath == null) {
            File file = new File(SDCardUtil.getDefaultPath(context), CACHE_PATH_IMAGE);
            imageCachePath = file.getAbsolutePath();
            if (!file.exists() && !file.mkdirs()) {
                imageCachePath = SDCardUtil.getDefaultPath(context);
            }
        }
        return imageCachePath;
    }
}
